#include "stdio.h"

//-------------------------插入------------------------
DLNode* insert(DLNode *head, DLNode *tail, int *num)
{
	int type;
	char number[20];
	time_t timep;

    if (head == NULL)
	{
	    return NULL;
	}

	if(tail == NULL || tail == head) //第一次插入判断 || 删除库中最后一个的数据后继续插入
	{
	    tail = head;
	    DLNode *Node = (DLNode *)malloc(sizeof(DLNode));
	    if (Node == NULL)
	    {
	        return NULL;
	    }
	    printf("当前剩余车位：%d\n",MAX-(*num));


	    printf("请输入汽车类型：1：小汽车，2：中巴车，3：客车，4：货车\n");
	    scanf("%d", &type);
		if(type != 1 && type != 2 && type != 3 && type != 4)
		{
		    printf("类型输入错误！\n");
			getchar();
			getchar();
			return NULL;
		}
	    printf("请输入汽车牌照：如皖A00001\n");
	    scanf("%s", number);

	    timep = time(&timep);
	    Node->type = type;
	    strcpy(Node->number,number);
	    strcpy(Node->time,ctime(&timep));
		Node->t = timep;
		(*num)++;
	
	    head->next = Node;
	    Node->prev = head;
	    Node->next = NULL;
	    tail = Node;
	}
	while(1)
	{
	    printf("当前剩余车位：%d\n",MAX-(*num));
		if(*num >= MAX)
		{
		    printf("车库已满!");
			return NULL;
		}

	    printf("请输入汽车类型：1：小汽车，2：中巴车，3：客车，4：货车。（输入9999结束）\n");
		scanf("%d", &type);
		if(type == 9999)
		{
		    break;
		}
		if(type != 1 && type != 2 && type != 3 && type != 4)
		{
		    printf("类型输入错误！\n");
			getchar();
			getchar();
			return NULL;
		}
	    printf("请输入汽车牌照：如皖A00001\n");
	    scanf("%s", number);

	    DLNode *Node = (DLNode *)malloc(sizeof(DLNode));
	    if (Node == NULL)
	    {
	        return NULL;
	    }
    	timep = time(&timep);
	    Node->type = type;
	    strcpy(Node->number,number);
	    strcpy( Node->time,ctime(&timep));
		Node->t = timep;

        (*num)++;

		tail->next = Node;
		Node->prev = tail;
		Node->next = NULL;
		tail = Node;

	}

	return tail;
}

//------------------------判空-----------------
int isEmpty(DLNode *head)
{
    if(head == NULL || head->next == NULL)
	{
	    printf("没有任何记录哦！\n");
		return -1;
	}
	
	return 0;
}

//---------------输出------------------
int Printf(DLNode *head)
{
    if(head == NULL)
	{
	    return -1;
	}

	DLNode *Node =(DLNode *)malloc(sizeof(DLNode));
	if(Node == NULL)
	{
	    return -1;
	}

    Node = head->next;

	while(Node != NULL)
	{
	    char type[20];
		switch (Node->type)
		{
		    case 1:
			{
			    strcpy(type,"小汽车");
				break;
			}
		    case 2:
			{
			    strcpy(type,"中巴车");
				break;
			}
		    case 3:
			{
			    strcpy(type,"客车");
				break;
			}
		    case 4:
			{
			    strcpy(type,"货车");
				break;
			}
			default :
			{
			    printf("类型错误！\n");
		        Node = Node->next;
				continue;
			}
		}

		printf("%s \t %s \t %s\n", type,Node->number,Node->time);
		Node = Node->next;
	}

	return 0;
}

//------------------------------删除----------------------
DLNode* Delete(DLNode *table, char number[20], DLNode *tail,int *num)  //返回tail用于处理库中只有一个数据删除后的处理
{
    if(table == NULL)
	{
	    return NULL;
	}

	DLNode *Node = table->next;
	while(Node != NULL)
	{
	    if(strcmp(Node->number,number) == 0)
		{  

		    time_t timen;
			timen = time(&timen);

		    DLNode *p = Node;
			if(Node->next != NULL)
			{
		        Node->prev->next = Node->next;
			    Node->next->prev = Node->prev;
			}
			else
			{
			    Node->prev->next = NULL;
				tail = Node->prev;  //尾指针处理
				tail->next = NULL;
			}


		    printf("车辆信息:%s\n", Node->number);
			printf("入库时间:%s出库时间:%s\n",Node->time,ctime(&timen));
			printf("需缴纳费用:%.1f元\n",difftime(timen,Node->t)*MONEY);
			printf("已出库!");
			(*num)--;

            if(p != NULL)
			{
			    free(p);
				p = NULL;
			}
			return tail;
		}
	    Node = Node->next;
	}

	printf("没有该车辆!\n");
	return NULL;
}

int Check(DLNode *head)
{
    if(head == NULL)
	{
	    return 0;
	}

	int num[4];
	memset(num,0,16);
	int all = 0;

	DLNode *pNode = head->next;
	while (pNode != NULL)
	{
	    switch(pNode->type)
		{
		    case 1:
			{
			    num[0]++;
				break;
			}
			case 2:
			{
			    num[1]++;
				break;
			}
			case 3:
			{
			    num[2]++;
				break;
			}
			case 4:
			{
			    num[3]++;
				break;
			}
			default:
			{
			    break;
			}
		}
	    pNode = pNode->next;
	}

	all = num[0]+num[1]+num[2]+num[3];
	printf("当前车库总车位为：%d，现已有 %d 辆车，其中：\n",MAX,all);
	printf("小汽车 %d 辆，中巴车 %d 辆，客车 %d 辆，货车 %d 辆\n",num[0],num[1],num[2],num[3]);

	return 1;
}


int CheckByNum(DLNode *table, char number[20])
{
    if(table == NULL)
	{
	    return 0;
	}

	DLNode *Node = table->next;
	while(Node != NULL)
	{
	    if(strcmp(Node->number,number) == 0)
		{  

		    time_t timen;
			timen = time(&timen);

		    printf("车辆信息:%s\n", Node->number);
			printf("入库时间:%s\n",Node->time);
			printf("如果现在出库需缴纳费用:%.1f元\n",difftime(timen,Node->t)*MONEY);

			return 1;
		}
	    Node = Node->next;
	}

	printf("没有该车辆!\n");
	return 0;
}
































